package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.noear.solon.core.handle.*;

/**
 * @author airhead
 */
@Slf4j
public abstract class BaseFilter implements Filter {
  @Override
  public void doFilter(Context ctx, FilterChain chain) throws Throwable {
    Action action = getAction(ctx);
    if (isClear(action)) {
      chain.doFilter(ctx);
      return;
    }

    doFilter(ctx, action, chain);
  }

  /**
   * 如果不想被忽略时，重写直接返回false
   *
   * @param action
   * @return
   */
  protected boolean isClear(Action action) {
    return false;
  }

  /**
   * 内部的过滤处理
   *
   * @param ctx
   * @param action
   * @param chain
   * @throws Throwable
   */
  protected abstract void doFilter(Context ctx, Action action, FilterChain chain) throws Throwable;

  protected Action getAction(Context ctx) {
    return ctx.action();
  }
}
